package hot_100;

import java.util.ArrayList;
import java.util.Arrays;

public class 完全平方数 {
    public static void main(String[] args) {
        System.out.println(numSquares(12));
    }

    public static int numSquares(int n) {
        ArrayList<Integer> a = new ArrayList<>();
        for (int i = 1; i <= n; i++) {
            if (i * i <= n) {
                a.add(i * i);
            }
        }
        int[] dp = new int[n + 1];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;
        for (int i = 1; i <= n; i++) {
            for (Integer num : a) {
                if (i - num >= 0) {
                    dp[i] = Math.min(dp[i], dp[i - num] + 1);
                } else {
                    break;
                }
            }
        }
        return dp[n];
    }
}
